package com.example.remotecontrol.ui.settings

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.DarkMode
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Notifications
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Vibration
import androidx.compose.material.icons.filled.VolumeUp
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Divider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.remotecontrol.BuildConfig
import com.example.remotecontrol.data.repository.SettingsRepository
import kotlinx.coroutines.launch

/**
 * 设置界面
 */
@Composable
fun SettingsScreen(
    viewModel: SettingsViewModel = hiltViewModel()
) {
    val vibrationEnabled by viewModel.vibrationEnabled.collectAsState()
    val soundEffectsEnabled by viewModel.soundEffectsEnabled.collectAsState()
    val autoScanEnabled by viewModel.autoScanEnabled.collectAsState()
    val notificationsEnabled by viewModel.notificationsEnabled.collectAsState()
    val themeMode by viewModel.themeMode.collectAsState()
    
    val snackbarHostState = remember { SnackbarHostState() }
    val coroutineScope = rememberCoroutineScope()
    
    var showResetDialog by remember { mutableStateOf(false) }
    var showAboutDialog by remember { mutableStateOf(false) }
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("设置") }
            )
        },
        snackbarHost = { SnackbarHost(snackbarHostState) }
    ) { padding ->
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(padding)
                .verticalScroll(rememberScrollState())
        ) {
            // 一般设置
            SettingsCategory(title = "通用设置")
            
            // 主题设置
            SettingsSwitch(
                title = "深色主题",
                description = "启用深色主题",
                icon = Icons.Default.DarkMode,
                checked = themeMode == SettingsRepository.ThemeMode.DARK,
                onCheckedChange = { enabled ->
                    viewModel.setThemeMode(
                        if (enabled) SettingsRepository.ThemeMode.DARK
                        else SettingsRepository.ThemeMode.LIGHT
                    )
                }
            )
            
            // 震动反馈
            SettingsSwitch(
                title = "按键震动",
                description = "按下按钮时震动",
                icon = Icons.Default.Vibration,
                checked = vibrationEnabled,
                onCheckedChange = { viewModel.setVibrationEnabled(it) }
            )
            
            // 音效
            SettingsSwitch(
                title = "按键音效",
                description = "按下按钮时播放声音",
                icon = Icons.Default.VolumeUp,
                checked = soundEffectsEnabled,
                onCheckedChange = { viewModel.setSoundEffectsEnabled(it) }
            )
            
            Divider(modifier = Modifier.padding(vertical = 8.dp))
            
            // 功能设置
            SettingsCategory(title = "功能设置")
            
            // 自动扫描
            SettingsSwitch(
                title = "自动扫描设备",
                description = "应用启动时自动扫描可用设备",
                icon = Icons.Default.Search,
                checked = autoScanEnabled,
                onCheckedChange = { viewModel.setAutoScanEnabled(it) }
            )
            
            // 通知
            SettingsSwitch(
                title = "通知提醒",
                description = "显示场景执行和设备连接状态的通知",
                icon = Icons.Default.Notifications,
                checked = notificationsEnabled,
                onCheckedChange = { viewModel.setNotificationsEnabled(it) }
            )
            
            Divider(modifier = Modifier.padding(vertical = 8.dp))
            
            // 关于和重置
            SettingsCategory(title = "其他")
            
            // 关于
            SettingsButton(
                title = "关于",
                description = "版本信息和声明",
                icon = Icons.Default.Info,
                onClick = { showAboutDialog = true }
            )
            
            // 重置设置
            SettingsButton(
                title = "重置设置",
                description = "恢复所有设置到默认值",
                onClick = { showResetDialog = true }
            )
            
            Spacer(modifier = Modifier.height(16.dp))
        }
        
        // 重置设置确认对话框
        if (showResetDialog) {
            AlertDialog(
                onDismissRequest = { showResetDialog = false },
                title = { Text("重置设置") },
                text = { Text("确定要将所有设置恢复到默认值吗？此操作无法撤销。") },
                confirmButton = {
                    TextButton(
                        onClick = {
                            viewModel.resetToDefaults()
                            showResetDialog = false
                            coroutineScope.launch {
                                snackbarHostState.showSnackbar("设置已重置")
                            }
                        }
                    ) {
                        Text("重置")
                    }
                },
                dismissButton = {
                    TextButton(onClick = { showResetDialog = false }) {
                        Text("取消")
                    }
                }
            )
        }
        
        // 关于对话框
        if (showAboutDialog) {
            AlertDialog(
                onDismissRequest = { showAboutDialog = false },
                title = { Text("关于") },
                text = {
                    Column {
                        Text("万能遥控器")
                        Text("版本: ${BuildConfig.VERSION_NAME}")
                        Spacer(modifier = Modifier.height(8.dp))
                        Text("一款功能强大的遥控器应用，支持红外、蓝牙和WiFi控制。")
                    }
                },
                confirmButton = {
                    TextButton(onClick = { showAboutDialog = false }) {
                        Text("确定")
                    }
                }
            )
        }
    }
} 